'''
CIO Object for SpcApp

'''

import numpy as np
from enum import Enum

class XYTAG(Enum):
    kXUP = 1
    kXDOWN = 2
    kYLEFT = 3
    kYRIGHT = 4

class IoValue:
    __now = 0
    __prior = 0
    __diff = 0

    def setNow(self, value):
        self.__prior = self.__now
        self.__now = value
        self.__diff = self.__prior - self.__now
        return
    def Now(self):
        return self.__now
    def Prior(self):
        return self.__prior
    def Diff(self):
        return self.__diff



class SpcCio:
    __DevName = ''
    INDEXIO = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

    srcIo = []   # sroce of IO datas, count is 16
    appIo = []   # app of IO datas, count is 16
    xValueUp = IoValue()
    xValueDown = IoValue()
    yValueLeft = IoValue()
    yValueRight = IoValue()


    __MaskIO = 0  # Use to mask io Index

    def __init__(self, devname):
        self.__DevName = devname
        self.srcIo = np.zeros((16),np.uint8)
        print("create CIO Object: ",self.__DevName)
        return
    def __del__(self):
        print("delete CIO Object: ", self.__DevName)
        return

    def setFristIO(self, ionum):
        if not ionum in self.INDEXIO:
            print("error in INDEXIO :from setFristIO")
            return
        self.__MaskIO = ionum
        return self.__MaskIO

    def printSrcDatas(self):
        print(self.srcIo)
        return

    def getAppIoIndex(self,appio):
        result = appio + self.__MaskIO
        if result >= 16:
            result = result - 16
        return result

    def getAppIo(self, index = 0):
        ind = self.getAppIoIndex(index)
        return self.srcIo[ind]

    def transDatasToAppData(self):
        for i in self.INDEXIO:
            self.appIo[i] = self.getAppIo(i)

    def calcXYIoValue(self, xory = XYTAG()):
        K = (-4,-3,-2,-1,0,1,2,3,4)
        lst = []
        tmp = 0
        ki=0
        if xory == XYTAG.kXUP:
            for i in [8, 7, 6, 5, 4, 3, 2, 1, 0]:
                tmp = tmp + self.getAppIo(i) * K[ki]
                ki += 1
        elif xory == XYTAG.kXDOWN:
            for i in [8, 9, 10, 11, 12, 13, 14, 15, 0]:
                tmp = tmp + self.getAppIo(i) * K[ki]
                ki += 1
        elif xory == XYTAG.kYLEFT:
            for i in [12, 11, 10, 9, 8, 7, 6, 5, 4]:
                tmp = tmp + self.getAppIo(i) * K[ki]
                ki += 1
        elif xory == XYTAG.kYRIGHT:
            for i in [12, 13, 14, 15, 0, 1, 2, 3, 4]:
                tmp = tmp + self.getAppIo(i) * K[ki]
                ki += 1
        else:
            tmp = 0

        return tmp

    def analyzeXYIoValue(self):
        self.xValueUp.setNow(self.calcXYIoValue(XYTAG.kXUP))
        self.xValueDown.setNow(self.calcXYIoValue(XYTAG.kXDOWN))
        self.yValueLeft.setNow(self.calcXYIoValue(XYTAG.kYLEFT))
        self.yValueRight.setNow(self.calcXYIoValue(XYTAG.kYRIGHT))

        Vx = (self.xValueUp.Diff() + self.xValueDown.Diff()) / 2
        Vy = (self.yValueLeft.Diff() + self.yValueRight.Diff()) / 2
        print("speed Vx:{0}  Vy:{1}".format(Vx, Vy))

        return

    def calcOffsetAngle(self):
        '''
        计算补偿角，只在1 4象限找节点计算

        :return :
        '''

        return



if __name__ == "__main__":

    a = SpcCio("testIO")
    lst = a.getAppIo([1,2,5])
    print(lst)
